auto.waitFor()
if (files.cwd().endsWith("脚本")) {
  let _require = require;
  require = function (path) {
    if (path.startsWith("./")) {
      path = path.replace("./", "");
    }
    path = files.cwd() + "/gdt_hb/" + path;
    return _require(path);
  }
}



var storage = storages.create("xiangxiangyuedu");
var time = random(2000, 3000)
var utils = require("./utils.js");

function launchAppEnhance(app_name) {
  let thread = threads.start(function () {
    toastLog("检查是否有打开权限!")
    while (true) {
      var allow = text("允许").findOne(5000)
      if (allow) {
        sleep(1000)
        toastLog("发现允许授权!")
        allow.click()
        sleep(1000)
        thread.interrupt()
        break
      }
    }
  })
  sleep(1000)
  var result = launchApp(app_name)
  sleep(3000)
  thread.interrupt()
  return result
}

// 点击控件坐标，控件的clickbale为false的时候
function 点击控件(x) {
  var weight = x.bounds()
  click(weight.centerX(), weight.centerY())
}

function task() {
  var 登录 = text('登  录').findOne(15000)
  if (登录) {
    sleep(time)
    toastLog('当前在登录界面')
    sleep(time)
    var tel = className('android.widget.EditText').depth(12).findOne(5000)
    if (tel) {
      tel.click()
      sleep(time)
      if (device.model == 'Redmi 8A') {
        click(random(96, 663), random(609, 674))
      }
      if (device.model == 'MI 5X') {
        click(random(165, 800), random(940, 1029))
      }
      sleep(time)
      登录.click()
      sleep(time)
    }
  }
  var 挖矿 = text('挖矿').findOne(5000)
  if (挖矿) {
    sleep(time)
    挖矿.click()
    sleep(time)
    var 进入矿场 = textStartsWith('进入矿场').findOne(5000)
    if (进入矿场) {
      进入矿场.click()
      sleep(time)
      var 体验矿机 = text('体验矿机').find()
      if (体验矿机.length > 0) {
        点击控件(体验矿机[1])
        sleep(5000)
        var 今日已领取完 = text('今日已领取完!').findOne(2000)
        if (今日已领取完) {
          toastLog('已经完成')
          sleep(time)
        } else {
          var index = 0
          while (index < 40) {
            var nums = textStartsWith('0.00').findOne(1000)
            if (nums) {
              nums.parent().click()
              sleep(time)
            } else {
              toastLog('挖矿完成')
              sleep(time)
              index = 100
            }
            index++
            utils.updateAppProgress(index*2.5);
          }
          sleep(time)
          if (device.model == 'Redmi 8A') {
            click(random(550, 656), random(1347, 1453))
          }
          if (device.model == 'MI 5X') {
            click(random(810, 977), random(1654, 1809))
          }
        }
      }
    }
  }
}

function main() {
  let open_app = launchAppEnhance("IPFS");
  if (open_app) {
    task()
  }
  else {
    toastLog("IPFS打开失败，请确认是否安装并授权打开!");
  }
}

try {
  main();
  utils.updateAppProgress(100);
} catch (error) {
  toastLog(error);
}

storage.put("script_running_status", "end");
